package com.zxy.activemqjms;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2020/11/22
 */
public class TopicConsumer {

    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

        Connection connection = connectionFactory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic1 = session.createTopic("topic1");

        MessageConsumer consumer = session.createConsumer(topic1);

        consumer.setMessageListener(message->{
                if(message!=null && message instanceof TextMessage)
                {
                    TextMessage textMessage = (TextMessage)message;
                    try {
                        String text = textMessage.getText();
                        System.out.println(text);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
        });
    }
}
